package com.qf.flash.ui.chat;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.R;
import com.qf.flash.event.ChangeGroupEvent;
import com.qf.flash.model.GroupDetailBean;
import com.qf.flashlib.base.BaseActivity;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChatGroupDetailsActivity extends BaseActivity {
    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_msg1)
    TextView tvMsg1;
    @BindView(R.id.tv_msg2)
    TextView tvMsg2;
    @BindView(R.id.tv_msg3)
    TextView tvMsg3;
    @BindView(R.id.tv_msg4)
    TextView tvMsg4;
    @BindView(R.id.tv_msg5)
    TextView tvMsg5;
    @BindView(R.id.tv_msg6)
    TextView tvMsg6;
    @BindView(R.id.tv_msg7)
    TextView tvMsg7;
    @BindView(R.id.tv_logout_group)
    TextView tvLogoutGroup;
    private Dialog dialog;
    private GroupDetailBean detailBean;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_chat_group_details;
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("组队详情");
        detailBean= (GroupDetailBean) getIntent().getSerializableExtra("detailBean");
        initUi();

    }



    public static void start(Activity activity, GroupDetailBean detailBean) {
        Intent intent = new Intent(activity, ChatGroupDetailsActivity.class);
        intent.putExtra("detailBean",detailBean);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_top_back, R.id.tv_logout_group})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.tv_logout_group:// 修改组队信息
                if(UserMgr.getInstance().getLoginUser().getUid().equals(detailBean.getCaptainId())){
                   ChangeGroupActivity.start(this, detailBean);
                }else {
                    dialog.show();
                }
                break;
            default:
                break;
        }
    }

    private void initUi() {
        tvMsg1.setText("队名："+detailBean.getTeamName());
        tvMsg2.setText("时间："+detailBean.getGameTime());
        tvMsg3.setText("密码："+detailBean.getGamePassword());
        tvMsg4.setText("题材："+detailBean.getPlayThemes());
        tvMsg5.setText("人数："+detailBean.getNeedNum()+"人");
        tvMsg6.setText("剧本："+detailBean.getPlayName());
        tvMsg7.setText("商家："+detailBean.getShopName());
        dialog = new Dialog(this, R.style.custom_dialog2);
        dialog.setCanceledOnTouchOutside(false);
        setFinishOnTouchOutside(true);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);

        View v = View.inflate(this, R.layout.dialog_modify_team_msg, null);
        window.setContentView(v);
//        window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏
        WindowManager.LayoutParams p = window.getAttributes();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay();
        p.width = (int) (d.getWidth() * 0.82);
        p.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(p);

        TextView tip = v.findViewById(R.id.tv_tips);
        TextView know = v.findViewById(R.id.tv_know);
        know.setOnClickListener(view->{
            dialog.dismiss();
        });
        String uid = UserMgr.getInstance().getLoginUser().getUid();
        if(uid.equals(detailBean.getCaptainId())){

        }else {
            List<GroupDetailBean.UsersBean> users = detailBean.getUsers();
            for (GroupDetailBean.UsersBean user : users) {
                if(user.getId().equals(detailBean.getCaptainId())){

                    tip.setText("只有队伍建立者 "+user.getName()+" 才能修改组队信息。");
                }
            }
        }

    }

    @Override
    protected boolean useEvent() {
        return true;
    }
    @Subscribe
    public void onChangeGroupEvent(ChangeGroupEvent event) {
        finish();
    }
}
